package com.innovate.nms.view;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.innovate.nms.domain.AppConfig;
import com.ivan.toolkit.swing.ConfigDialog;
import com.ivan.toolkit.swing.PromptTextField;

public class ConfigAppDialog extends ConfigDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2303310124097507296L;
	private PromptTextField tfLocalName;
	private PromptTextField tfRtuName;
	private PromptTextField tfRtuIP;
	private PromptTextField tfEmu1Name;
	private PromptTextField tfEmu1IP;
	private PromptTextField tfEmu1SMS;
	private PromptTextField tfEmu2Name;
	private PromptTextField tfEmu2IP;
	private PromptTextField tfEmu2SMS;
	private PromptTextField tfEmu3Name;
	private PromptTextField tfEmu3IP;
	private PromptTextField tfEmu3SMS;


	/**
	 * Create the dialog.
	 */
	public ConfigAppDialog() {
		setTitle("监测配置");
		setBounds(100, 100, 556, 623);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{60, 100, 200, 0};
		gbl_panel.rowHeights = new int[]{60, 0, 10, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JLabel label = new JLabel("本地描述：");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 1;
		gbc_label.gridy = 1;
		panel.add(label, gbc_label);
		
		tfLocalName = new PromptTextField();
		tfLocalName.getNullLabel().setText("请输入本地描述");
		GridBagConstraints gbc_tfLocalName = new GridBagConstraints();
		gbc_tfLocalName.insets = new Insets(0, 0, 5, 0);
		gbc_tfLocalName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfLocalName.gridx = 2;
		gbc_tfLocalName.gridy = 1;
		panel.add(tfLocalName, gbc_tfLocalName);
		
		JLabel lblRtu = new JLabel("RTU 描述：");
		GridBagConstraints gbc_lblRtu = new GridBagConstraints();
		gbc_lblRtu.anchor = GridBagConstraints.EAST;
		gbc_lblRtu.insets = new Insets(0, 0, 5, 5);
		gbc_lblRtu.gridx = 1;
		gbc_lblRtu.gridy = 3;
		panel.add(lblRtu, gbc_lblRtu);
		
		tfRtuName = new PromptTextField();
		GridBagConstraints gbc_tfRtuName = new GridBagConstraints();
		gbc_tfRtuName.insets = new Insets(0, 0, 5, 0);
		gbc_tfRtuName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfRtuName.gridx = 2;
		gbc_tfRtuName.gridy = 3;
		panel.add(tfRtuName, gbc_tfRtuName);
		
		JLabel lblRtuIp = new JLabel("RTU IP：");
		GridBagConstraints gbc_lblRtuIp = new GridBagConstraints();
		gbc_lblRtuIp.insets = new Insets(0, 0, 5, 5);
		gbc_lblRtuIp.anchor = GridBagConstraints.EAST;
		gbc_lblRtuIp.gridx = 1;
		gbc_lblRtuIp.gridy = 4;
		panel.add(lblRtuIp, gbc_lblRtuIp);
		
		tfRtuIP = new PromptTextField();
		GridBagConstraints gbc_tfRtuIP = new GridBagConstraints();
		gbc_tfRtuIP.insets = new Insets(0, 0, 5, 0);
		gbc_tfRtuIP.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfRtuIP.gridx = 2;
		gbc_tfRtuIP.gridy = 4;
		panel.add(tfRtuIP, gbc_tfRtuIP);
		
		JLabel lblEmu = new JLabel("EMU1 描述：");
		GridBagConstraints gbc_lblEmu = new GridBagConstraints();
		gbc_lblEmu.anchor = GridBagConstraints.EAST;
		gbc_lblEmu.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmu.gridx = 1;
		gbc_lblEmu.gridy = 6;
		panel.add(lblEmu, gbc_lblEmu);
		
		tfEmu1Name = new PromptTextField();
		GridBagConstraints gbc_tfEmu1Name = new GridBagConstraints();
		gbc_tfEmu1Name.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu1Name.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu1Name.gridx = 2;
		gbc_tfEmu1Name.gridy = 6;
		panel.add(tfEmu1Name, gbc_tfEmu1Name);
		
		JLabel lblEmuIp = new JLabel("EMU1 IP：");
		GridBagConstraints gbc_lblEmuIp = new GridBagConstraints();
		gbc_lblEmuIp.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmuIp.anchor = GridBagConstraints.EAST;
		gbc_lblEmuIp.gridx = 1;
		gbc_lblEmuIp.gridy = 7;
		panel.add(lblEmuIp, gbc_lblEmuIp);
		
		tfEmu1IP = new PromptTextField();
		GridBagConstraints gbc_tfEmu1IP = new GridBagConstraints();
		gbc_tfEmu1IP.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu1IP.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu1IP.gridx = 2;
		gbc_tfEmu1IP.gridy = 7;
		panel.add(tfEmu1IP, gbc_tfEmu1IP);
		
		JLabel lblEmuSms = new JLabel("EMU1 SMS：");
		GridBagConstraints gbc_lblEmuSms = new GridBagConstraints();
		gbc_lblEmuSms.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmuSms.anchor = GridBagConstraints.EAST;
		gbc_lblEmuSms.gridx = 1;
		gbc_lblEmuSms.gridy = 8;
		panel.add(lblEmuSms, gbc_lblEmuSms);
		
		tfEmu1SMS = new PromptTextField();
		GridBagConstraints gbc_tfEmu1SMS = new GridBagConstraints();
		gbc_tfEmu1SMS.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu1SMS.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu1SMS.gridx = 2;
		gbc_tfEmu1SMS.gridy = 8;
		panel.add(tfEmu1SMS, gbc_tfEmu1SMS);
		
		JLabel lblEmu_1 = new JLabel("EMU2 描述：");
		GridBagConstraints gbc_lblEmu_1 = new GridBagConstraints();
		gbc_lblEmu_1.anchor = GridBagConstraints.EAST;
		gbc_lblEmu_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmu_1.gridx = 1;
		gbc_lblEmu_1.gridy = 10;
		panel.add(lblEmu_1, gbc_lblEmu_1);
		
		tfEmu2Name = new PromptTextField();
		GridBagConstraints gbc_tfEmu2Name = new GridBagConstraints();
		gbc_tfEmu2Name.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu2Name.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu2Name.gridx = 2;
		gbc_tfEmu2Name.gridy = 10;
		panel.add(tfEmu2Name, gbc_tfEmu2Name);
		
		JLabel lblEmuIp_1 = new JLabel("EMU2 IP：");
		GridBagConstraints gbc_lblEmuIp_1 = new GridBagConstraints();
		gbc_lblEmuIp_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmuIp_1.anchor = GridBagConstraints.EAST;
		gbc_lblEmuIp_1.gridx = 1;
		gbc_lblEmuIp_1.gridy = 11;
		panel.add(lblEmuIp_1, gbc_lblEmuIp_1);
		
		tfEmu2IP = new PromptTextField();
		GridBagConstraints gbc_tfEmu2IP = new GridBagConstraints();
		gbc_tfEmu2IP.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu2IP.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu2IP.gridx = 2;
		gbc_tfEmu2IP.gridy = 11;
		panel.add(tfEmu2IP, gbc_tfEmu2IP);
		
		JLabel lblEmuIp_2 = new JLabel("EMU2 SMS：");
		GridBagConstraints gbc_lblEmuIp_2 = new GridBagConstraints();
		gbc_lblEmuIp_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmuIp_2.anchor = GridBagConstraints.EAST;
		gbc_lblEmuIp_2.gridx = 1;
		gbc_lblEmuIp_2.gridy = 12;
		panel.add(lblEmuIp_2, gbc_lblEmuIp_2);
		
		tfEmu2SMS = new PromptTextField();
		GridBagConstraints gbc_tfEmu2SMS = new GridBagConstraints();
		gbc_tfEmu2SMS.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu2SMS.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu2SMS.gridx = 2;
		gbc_tfEmu2SMS.gridy = 12;
		panel.add(tfEmu2SMS, gbc_tfEmu2SMS);
		
		JLabel lblEmu_2 = new JLabel("EMU3 描述：");
		GridBagConstraints gbc_lblEmu_2 = new GridBagConstraints();
		gbc_lblEmu_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmu_2.anchor = GridBagConstraints.EAST;
		gbc_lblEmu_2.gridx = 1;
		gbc_lblEmu_2.gridy = 14;
		panel.add(lblEmu_2, gbc_lblEmu_2);
		
		tfEmu3Name = new PromptTextField();
		GridBagConstraints gbc_tfEmu3Name = new GridBagConstraints();
		gbc_tfEmu3Name.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu3Name.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu3Name.gridx = 2;
		gbc_tfEmu3Name.gridy = 14;
		panel.add(tfEmu3Name, gbc_tfEmu3Name);
		
		JLabel lblEmuIp_3 = new JLabel("EMU3 IP：");
		GridBagConstraints gbc_lblEmuIp_3 = new GridBagConstraints();
		gbc_lblEmuIp_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmuIp_3.anchor = GridBagConstraints.EAST;
		gbc_lblEmuIp_3.gridx = 1;
		gbc_lblEmuIp_3.gridy = 15;
		panel.add(lblEmuIp_3, gbc_lblEmuIp_3);
		
		tfEmu3IP = new PromptTextField();
		GridBagConstraints gbc_tfEmu3IP = new GridBagConstraints();
		gbc_tfEmu3IP.insets = new Insets(0, 0, 5, 0);
		gbc_tfEmu3IP.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu3IP.gridx = 2;
		gbc_tfEmu3IP.gridy = 15;
		panel.add(tfEmu3IP, gbc_tfEmu3IP);
		
		JLabel lblEmuSms_1 = new JLabel("EMU3 SMS：");
		GridBagConstraints gbc_lblEmuSms_1 = new GridBagConstraints();
		gbc_lblEmuSms_1.insets = new Insets(0, 0, 0, 5);
		gbc_lblEmuSms_1.anchor = GridBagConstraints.EAST;
		gbc_lblEmuSms_1.gridx = 1;
		gbc_lblEmuSms_1.gridy = 16;
		panel.add(lblEmuSms_1, gbc_lblEmuSms_1);
		
		tfEmu3SMS = new PromptTextField();
		GridBagConstraints gbc_tfEmu3SMS = new GridBagConstraints();
		gbc_tfEmu3SMS.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfEmu3SMS.gridx = 2;
		gbc_tfEmu3SMS.gridy = 16;
		panel.add(tfEmu3SMS, gbc_tfEmu3SMS);

	}


	@Override
	protected void prepare() throws Exception {
		AppConfig config = AppConfig.instance.get();
		this.tfLocalName.setText(config.getLocalName());
		this.tfRtuName.setText(config.getRtu().getName());
		this.tfRtuIP.setText(config.getRtu().getIp());
		this.tfEmu1Name.setText(config.getEmu1().getName());
		this.tfEmu1IP.setText(config.getEmu1().getIp());
		this.tfEmu1SMS.setText(config.getEmu1().getSms());
		this.tfEmu2Name.setText(config.getEmu2().getName());
		this.tfEmu2IP.setText(config.getEmu2().getIp());
		this.tfEmu2SMS.setText(config.getEmu2().getSms());
		this.tfEmu3Name.setText(config.getEmu3().getName());
		this.tfEmu3IP.setText(config.getEmu3().getIp());
		this.tfEmu3SMS.setText(config.getEmu3().getSms());
	}


	@Override
	protected void doCommit() throws Exception {

		AppConfig config = AppConfig.instance.get();
		config.setLocalName(toText(tfLocalName));
		config.getRtu().setName(toText(tfRtuName));
		config.getRtu().setIp(toText(tfRtuIP));
		config.getEmu1().setName(toText(tfEmu1Name));
		config.getEmu1().setIp(toText(tfEmu1IP));
		config.getEmu1().setSms(toText(tfEmu1SMS));
		config.getEmu2().setName(toText(tfEmu2Name));
		config.getEmu2().setIp(toText(tfEmu2IP));
		config.getEmu2().setSms(toText(tfEmu2SMS)); 
		config.getEmu3().setName(toText(tfEmu3Name));
		config.getEmu3().setIp(toText(tfEmu3IP));
		config.getEmu3().setSms(toText(tfEmu3SMS));
		AppConfig.instance.set();
	}
	
	
	
}
